import pygame

from Actors.Mine_Boom import Boom, Mine
from Dialog.Dialog_For_Systerm import TunnelTask2BeginDialog, TunnelTask2EndDialog, TunnelTaskFailDialog, \
    TunnelTask2MidBeginDialog, TunnelTaskFailFoundDialog
from Task.BaseTask import BaseTask, TaskStatus


class KillJapaneseTask(BaseTask):
    def __init__(self, lead_actor, japanese, captain, vice_captain, font):
        self.score = 0
        self.lead_actor = lead_actor
        self.japanese = japanese
        self.captain = captain
        self.vice_captain = vice_captain
        # 加载炸弹和爆炸图片
        self.mines = pygame.sprite.Group()
        self.booms = pygame.sprite.Group()
        mine = pygame.image.load("./Resources/images/mine/mine.png")
        self.mine_image = pygame.transform.scale(mine, (40, 40))
        self.boom_image = pygame.image.load("./Resources/images/mine/boom.png")
        # 加载系统字幕
        self.win_dialog = TunnelTask2EndDialog(font)
        self.fail_dialog = TunnelTaskFailDialog(font)
        self.task_dialog = TunnelTask2MidBeginDialog(font)
        self.fail_found_dialog = TunnelTaskFailFoundDialog(font)

        self.time_count = 1000
        super(KillJapaneseTask, self).__init__(self.task_dialog, self.win_dialog,
                                               self.fail_dialog, self.fail_found_dialog)

    def __create_boom(self, x, y):
        """
        创造爆炸效果
        :param x:
        :param y:
        :return:
        """
        mine = Boom(x, y, self.boom_image, 4, 3)
        self.booms.add(mine)

    def __create_mine(self, x, y):
        """
        创造炸弹
        :param x:
        :param y:
        :return:
        """
        mine = Mine(x, y, self.mine_image)
        self.mines.add(mine)

    def __do_collide_boom(self, key_down, key_list):
        """
        炸弹碰撞检测
        :return:
        """
        collisions = pygame.sprite.groupcollide(self.mines, self.japanese, True, True)
        for wall in collisions:
            self.__create_boom(wall.rect.x - wall.rect.width,
                               wall.rect.y - wall.rect.height)
            self.score += 1

        if key_down and key_list[pygame.K_SPACE]:
            self.__create_mine(self.lead_actor.pos_x, self.lead_actor.pos_y)

        for boom in self.booms:
            if boom.kill_flag():
                self.booms.remove(boom)

    def draw_task(self, current_surface, x, y):
        """
        绘制炸弹
        :param current_surface:
        :param x:
        :param y:
        :return:
        """
        self.captain.draw(current_surface, x, y)
        self.vice_captain.draw(current_surface, x, y)
        for mine in self.mines:
            mine.draw(current_surface, x, y)
        for boom in self.booms:
            boom.draw(current_surface, x, y)

        # self.captain.draw(current_surface, x, y)
        # if self.state == CollectTaskStatus.found_captain:
        #     self.captain_dialog.draw(current_surface, x, y)
        #     if self.captain_dialog.is_finish():
        #         self.state = CollectTaskStatus.finding_prize
        #
        # elif self.state == CollectTaskStatus.found_vice_captain:
        #     self.vice_captain_dialog.draw(current_surface, x, y)
        #     if self.vice_captain_dialog.is_finish():
        #         self.state = CollectTaskStatus.ok
        #         self.task_status = TaskStatus.win_dialog
        #
        # if self.state >= CollectTaskStatus.finding_vice_captain:
        #     self.vice_captain.draw(current_surface, x, y)

    def do_self_task(self, key_down, key_list):
        """
        任务的执行
        :param key_down:
        :param key_list:
        :return:
        """
        # 碰撞检测
        self.__do_collide_boom(key_down, key_list)
        # 如果炸死大于2
        if self.score > 2:
            self.task_status = TaskStatus.win_dialog
            self.lead_actor.set_status("army")
            # 这里可以加音效

        # 计时
        self.time_count -= 1
        if self.time_count <= 0:
            self.task_status = TaskStatus.fail_dialog

        # 是否被发现
        list_collide = pygame.sprite.spritecollide(self.lead_actor, self.japanese, False)
        if len(list_collide) > 0:
            self.task_status = TaskStatus.fail_found_dialog
